home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 9 / Night Owl CD-ROM (NOPV9) (Night Owl Publisher) (1993).ISO / 015a / uecho.zip / UECHO.C next >
C/C++ Source or Header  |  1993-04-12  |  8KB  |  265 lines

  1. /*****
  2. **     Author  : E. Heverly
  3. **     Date    : 03/01/93
  4. **     Purpose : Provide Unix like echo utility for DOS.
  5. **     Contact : E. Heverly
  6. **               28661 Palomino
  7. **               Warren, MI 48093 (313) 354-1370 - daytime
  8. **               (313) 558-9528 - Evenings
  9. **               (Sorry - no e-mail address)
  10. **
  11. **     Features: This utility provides the functions of the UNIX echo
  12. **               (shell builtin) including:
  13. **                 - No carriage returns          \c
  14. **                 - Octal notation printing      \nnn
  15. **                 - Alert (ring bell)            \a
  16. **                 - Backspace                    \b
  17. **                 - Formfeed                     \f
  18. **                 - Carriage Return (no newline) \r
  19. **                 - Vertical Tab                 \v
  20. **                 - Tab                          \t
  21. **                 - Print current directory list  *
  22. **
  23. **               Note: This utility is also helpful since if the following:
  24. **               uecho set VAR=1                  >> AUTOEXEC.BAT
  25. **               was placed in a .BAT file only the sed VAR=1 would be in
  26. **               in the AUTOEXEC.BAT, where the DOS echo command would put
  27. **               in the additional trailing spaces.
  28. **
  29. **
  30. **     Compile : I created this program using Microsoft (tm) Quick-C
  31. **               and compiled it with the same.  No special header files
  32. **               are required outside of what MS provides with Quick-C.
  33. **
  34. **     Testing : I have tested it in DOS 3.3 and 5.0.  This testing
  35. **               includes all special functions listed above as well as a
  36. **               standard echo text.
  37. **
  38. **     Example : C:\> uecho Hello \n\n\n Welcome \rWelcome\n \007
  39. **     Produces: Hello
  40. **
  41. **
  42. **               Welcome
  43. **                  <Computers Beep>
  44. **               C:\>
  45. **
  46. **     Notes   : MS, Microsoft, and Quick-C are trademarks of Microsoft.
  47. **               UNIX is a registered trademark of AT&T.
  48. **
  49. *****/
  50.  
  51. #include <dos.h>
  52. #include <stdlib.h>
  53. #include <string.h>
  54.  
  55. void print_help(void);
  56. void fileinfo( struct find_t *find );
  57.  
  58. int main(int argc, char *argv[])
  59. {
  60.  struct find_t find;
  61.  char *wildspec[]={"*.*"};
  62.  char _far *CmdLine;
  63.  char cmdline[120];
  64.  char swch;
  65.  char chr;
  66.  int oct;
  67.  char CC;
  68.  char *s;
  69.  char *x;
  70.  
  71.  int  i,
  72.       rtn=0,
  73.       o,
  74.       acc,
  75.       end=0,
  76.       newline=0,
  77.       cstrlen,
  78.       tmp=0;
  79.  
  80.   for (acc=0 ; acc < argc ; ++acc)
  81.        {
  82.     s=(argv[acc]);
  83.     if ( *s == '-' )
  84.        {
  85.        s=(++argv[acc]);
  86.        switch (*s)
  87.           {
  88.           case '?':                            /* if -? was on      */
  89.         print_help();                      /* the command line  */
  90.         break;                             /* print help        */
  91.           default:
  92.         break;
  93.           }
  94.        }
  95.        }
  96.  
  97.  FP_SEG(CmdLine) = _psp;                           /* segment address    */
  98.  FP_OFF(CmdLine) = 0x81;                           /* offset into segment*/
  99.  
  100.  cstrlen=(_fstrlen ( CmdLine ));
  101.  
  102.  for ( i=0; i <= cstrlen ; i++ )                    /* get the command   */
  103.     {                                               /* line far into     */
  104.     CC=*CmdLine++;                                  /* a usable string   */
  105.     if ( CC == '\r' )                               /* Convert upto the  */
  106.        {                                            /* first carriage    */
  107.        end=1;                                       /* return in far     */
  108.        cmdline[i]='\0';                             /* & set end of      */
  109.        }                                            /* string            */
  110.     else                                            /*                   */
  111.        {                                            /*                   */
  112.        if ( end == 0 )                              /*                   */
  113.       cmdline[i] = CC;                          /*                   */
  114.        }                                            /*                   */
  115.     }                                               /*                   */
  116.  
  117.  cstrlen=( strlen ( cmdline ));                     /* Get input length  */
  118.  
  119.  CC='N';
  120.  o=cstrlen;
  121.  o--;
  122.  for ( i=o; i > 0 ; i-- )
  123.      {
  124.      if (cmdline[i] == 0x20 && CC == 'N'  )
  125.      cstrlen=i;
  126.      else
  127.      CC='Y';
  128.      }
  129.  
  130. /*     printf("cstrlen %u", cstrlen);       */
  131.  
  132.  for ( i=1 ; i < cstrlen ; i++)
  133.     {
  134.     if ( cmdline[i] == '\\' )
  135.     {
  136.     ++i;
  137.     swch=cmdline[i];
  138.     switch (toupper(swch))
  139.        {
  140.        case 'N':                            /* print newline         */
  141.           printf("\n");
  142.           break;
  143.        case 'R':                            /* print carriage return */
  144.           printf("\r");
  145.           break;
  146.        case 'T':                            /* print horz. tab       */
  147.           printf("\t");
  148.           break;
  149.        case '\"':                           /* print double quote    */
  150.           printf("\"");
  151.           break;
  152.        case '*':                            /* print asterisk        */
  153.           printf("*");
  154.           break;
  155.        case 'A':                            /* ring bell             */
  156.           printf("\a");
  157.           break;
  158.        case 'B':                            /* print backspace       */
  159.           printf("\b");
  160.           break;
  161.        case 'F':                            /* print form feed       */
  162.           printf("\f");
  163.           break;
  164.        case 'V':                            /* print vertical tab    */
  165.           printf("\v");
  166.           break;
  167.        case '\\':                           /* print backslash       */
  168.           printf("\\");
  169.           break;
  170.        case '\'':                           /* print single quote    */
  171.           printf("\'");
  172.           break;
  173.        case 'C':                            /* DON'T Print a newline */
  174.           newline=1;
  175.           break;
  176.        case '0':                            /* Print char in octal not.*/
  177.           oct=0;
  178.           chr=(cmdline[i + 3]);
  179.           if ( chr == ' '|| chr == '\0' )
  180.          {                            /* this side is for \nnn     */
  181.          oct=(oct + ((cmdline[++i] - '0') * 8 ));
  182.          oct=(oct +  (cmdline[++i] - '0'));
  183.          printf("%c", oct);
  184.          }
  185.           else
  186.          {                            /* this side is for \nnnn    */
  187.          oct=(oct + ((cmdline[++i] - '0') * 8 * 8));
  188.          oct=(oct + ((cmdline[++i] - '0') * 8 ));
  189.          oct=(oct +  (cmdline[++i] - '0'));
  190.          printf("%c", oct);
  191.          }
  192.           i++;
  193.           break;
  194.  
  195.        }
  196.     }
  197.     else
  198.     {
  199.     swch=cmdline[i];
  200.     switch (toupper(swch))
  201.        {
  202.        case '\"':                            /* DON'T print quotes    */
  203.         break;
  204.        case '*':                             /* print directory       */
  205.         printf("\n");
  206.         rtn=(_dos_findfirst(wildspec[0], 0xffff,  &find ) );
  207.         if ( rtn == 0 )
  208.              {
  209.              while( !_dos_findnext( &find ) )
  210.              fileinfo( &find );
  211.              }
  212.          else
  213.              printf("DOS error code: %d", rtn);
  214.          find;
  215.          break;
  216.        default:
  217.          printf("%c", cmdline[i]);       /* Print input characters*/
  218.          break;
  219.        }
  220.     }
  221.     }
  222.  
  223.  if ( newline == 0 )                            /* if /c was not entered */
  224.     printf("\n");                               /* print a newline       */
  225.  
  226.  exit(0);
  227. }
  228.  
  229.  
  230. /* Print *.* in curent directory similar to * in UNIX echo utility */
  231. void fileinfo( struct find_t *pfind )
  232. {
  233.     if( pfind->attrib & _A_SUBDIR )
  234.     printf ("[%-12s]  ", pfind->name );
  235.     else
  236.     printf( " %-12s   ", pfind->name );
  237. }
  238.  
  239.  
  240. void print_help(void)
  241. {
  242.   printf("Usage: uecho text <options> \n");
  243.   printf("Where: \\c - do not print a newline at the end\n");
  244.   printf("       \\a - alert (bell)\n");
  245.   printf("       \\b - backspace\n");
  246.   printf("       \\f - formfeed\n");
  247.   printf("       \\n - newline\n");
  248.   printf("       \\r - carriage return\n");
  249.   printf("       \\v - vertical tab - \v \n");
  250.   printf("       \\t - tab\n");
  251.   printf("       \\  - backslash\n");
  252.   printf("       \\\" - double quote\n");
  253.   printf("       \\' - single quote\n");
  254.   printf("       \\nnn - ascii notation of octal number\n");
  255.   printf("Examp: uecho Hello \\n Press Any Key \\c \n");
  256.   printf("       would produce Hello <return> Press Any KeyC:\\");
  257.   exit (0);
  258. }
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.